<?php

include_once("core/controller.php");
include_once("core/groups.php");
include_once("core/versionHandler.php");

/**
 * addUser
 *
 * @copyright inovia
 * @package oolime_manager 
 */
class AddUser extends Controller{
  
  public function isLegitimate(){
	
    // Inputs verifications
    if(!$this->request->exist(Array("step"))){
      return false;
    }else {
	    $tabVerify = Array(
  	    "login"  => Array("string","Please enter a username"),
  	    "mdp"    => Array("string","Please enter a password"),
  	    "mdp2"   => Array("string","Please confirm the password"),
  	    "email"  => Array("email","Invalid email"),
  	    "group"  => Array("string","Please choose a group")
  	  );
  	  if( (!$this->request->check($tabVerify)) || (! ($this->request->get("mdp") == $this->request->get("mdp2"))) ){
          $this->response->addError("Passwords are different","mdp2");
          return false;
      }
    }
    return true;
  }
  
  public function actions(){
    include_once("core/user.php");
    $checkuser = new User('r',"user",$this->request->get("login"));

    if($checkuser->isOk()){
      $this->response->addError("This user already exists","login");
      $this->defaultActions();
    }else{
      $newuser = new User('c');
      $newuser->range=array('id','user','password','email','group');
      $newuser->field->user = $this->request->get("login");
      $newuser->field->password = md5($this->request->get("mdp"));
	    $newuser->field->email = $this->request->get("email");
	    $newuser->field->group = $this->request->get("group");

	    //adds a node to say that it has been added
	    $Node = $this->response->doc->createElement("inserted");
	    $Value = $this->response->doc->createTextNode($this->request->get("login"));
      $Node->appendChild($Value);
      $this->response->root->appendChild($Node);
	    $newuser = null;
	    
	    $versionH=new VersionHandler("");
      $this->response->add($versionH->getMyVersion());
      include_once("controls/manager/homeUsers.php");
      new homeUsers();
    }

  }
  
  public function defaultActions(){
    $versionH=new VersionHandler("");
    $this->response->add($versionH->getMyVersion());
    
    $groupsNode = new Groups();
    $groupsNode->getGroups();
    $this->response->setXSLT("views/manager/addusers.xsl");
  }
  
  
}

?>
